import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../dd_core/dd_hive_dao.dart';
import '../../../dd_core/dd_user_service.dart';
import '../../../dd_models/dd_hive_comment_model.dart';
import '../../../dd_models/dd_hive_works_model.dart';
import '../../dd_video/controllers/dd_video_palyer_controller.dart';

class DDCommentController extends GetxController {
  FocusNode focusNode = FocusNode();

  HiveWorksModel commentModel = HiveWorksModel();

  TextEditingController control = TextEditingController();

  final ScrollController scrollController = ScrollController();

  final speakState = false.obs;

  final messageList = <HiveCommentModel>[].obs;


  @override
  void onClose() {
    speakState.close();
    focusNode.dispose();
    control.dispose();
    scrollController.dispose();
    messageList.close();
    super.onClose();
  }

  @override
  onInit() {
    super.onInit();
  }


  void scrollToBottom() {
    Future.delayed(Duration(milliseconds: 30), () {
      scrollController.animateTo(scrollController.position.maxScrollExtent,
          duration: Duration(microseconds: 300), curve: Curves.easeInOut);
    });
  }

  setModel(HiveWorksModel model) {
    commentModel = model;
  }

  Future getList(HiveWorksModel model) async {
    messageList.value =
        await DDHiveDao.getFencingCommentList(hiveWorksModel: model);
    messageList.refresh();
  }

  void sendMessage(HiveWorksModel model) async {
    if (control.text != "") {
      await addComment(model, 1, control.text);
      control.text = '';
    }
    if (Get.isRegistered<DDVideoPlayerController>()) {
      Get.find<DDVideoPlayerController>().getMessageList();
    }
  }

  void sendSound(HiveWorksModel model, String contnet , int time) async {
    await addComment(model, 2, contnet , soundTime:time);
    if (Get.isRegistered<DDVideoPlayerController>()) {
      Get.find<DDVideoPlayerController>().getMessageList();
    }
  }

  Future addComment(
      HiveWorksModel model, int messageType, String message, {int soundTime=0}) async {
    EasyLoading.show();
    await Future.delayed(Duration(seconds: 1));
    EasyLoading.dismiss();
    String name = Get.find<DDUserService>().authModel.userinfo?.nickname ?? '';
    String uid = Get.find<DDUserService>().authModel.userinfo?.userId ?? '';
    String iconUrl =
        Get.find<DDUserService>().authModel.userinfo?.avatarUrl ?? '';

    await DDHiveDao.updateFencingComment(
        fencingid: model.fencingid!,
        message: message,
        name: name,
        uid: uid,
        type: messageType,
        iconUrl: iconUrl,
        soundTime: soundTime);

    await getList(model);
    scrollToBottom();
  }

  updateMyLike(HiveCommentModel m) async {
    String fencingid = commentModel.fencingid ?? '';
    String messageId = m.messageId ?? '';
    bool myLike = true;
    if (m.myLikes != null) {
      myLike = !m.myLikes!;
    }
    await DDHiveDao.updateFencingCommentMyLike(
        fencingid: fencingid, messageId: messageId, myLike: myLike);
    await getList(commentModel);
  }
}
